#include "XMesh.h"
#include <fstream>
#include "XLog.h"
#include "../XModEngine/XFormat.h"
#include "../LibJson/reader.h"
using namespace XEFMT;
XMesh::XMesh(){
	m_indexed = NULL;
	m_vertex = NULL;

	u_face_count = 0;
	u_vert_count = 0;
}

XMesh::~XMesh(){
	for each (XSubMesh* ptr_mesh in m_meshes)
	{
		SAFE_RELEASE(ptr_mesh);
	}
	m_meshes.clear();
	if (m_indexed != NULL)
		m_indexed->Release();
	if (m_vertex != NULL)
		m_vertex->Release();
}

void XMesh::FillMesh(void* ptr_vert_data, int i_vert_len, void* ptr_index_data, int i_index_len){
	if (!XRender::GetInstance()->CreateVertexBuffer(&m_vertex, i_vert_len, true, false, 1))
		return;
	if (!XRender::GetInstance()->CreateIndexBuffer(&m_indexed, i_index_len, true, false, 16))
		return;
	m_vertex->SetSourceBuffer(ptr_vert_data, 0, i_vert_len);
	m_indexed->SetSourceBuffer(ptr_index_data, 0, i_index_len);

}




//void XMesh::MakeCube(){
//	Vector3* m_vertex = new Vector3[8];
//
//	m_vertex[0] = Vector3(-1.0f, -1.0f, -1.0f);
//	m_vertex[1] = Vector3(-1.0f, 1.0f, -1.0f);
//	m_vertex[2] = Vector3(1.0f, 1.0f, -1.0f);
//	m_vertex[3] = Vector3(1.0f, -1.0f, -1.0f);
//	m_vertex[4] = Vector3(-1.0f, -1.0f, 1.0f);
//	m_vertex[5] = Vector3(-1.0f, 1.0f, 1.0f);
//	m_vertex[6] = Vector3(1.0f, 1.0f, 1.0f);
//	m_vertex[7] = Vector3(1.0f, -1.0f, 1.0f);
//
//	WORD* indices = new WORD[36];
//
//	indices[0] = 0; indices[1] = 1; indices[2] = 2;
//	indices[3] = 0; indices[4] = 2; indices[5] = 3;
//
//	indices[6] = 4; indices[7] = 6; indices[8] = 5;
//	indices[9] = 4; indices[10] = 7; indices[11] = 6;
//
//	indices[12] = 4; indices[13] = 5; indices[14] = 1;
//	indices[15] = 4; indices[16] = 1; indices[17] = 0;
//
//	indices[18] = 3; indices[19] = 2; indices[20] = 6;
//	indices[21] = 3; indices[22] = 6; indices[23] = 7;
//
//	indices[24] = 1; indices[25] = 5; indices[26] = 6;
//	indices[27] = 1; indices[28] = 6; indices[29] = 2;
//
//	indices[30] = 4; indices[31] = 0; indices[32] = 3;
//	indices[33] = 4; indices[34] = 3; indices[35] = 7;
//
//	XSubMesh* ptr_submesh = new XSubMesh();
//	u_vert_count = 8;
//	u_face_count = 12;
//	FillMesh((void*)m_vertex, sizeof(Vector3)* 8, (void*)indices, sizeof(WORD)* 36);
//	m_meshes.insert(m_meshes.end(), ptr_submesh);
//	delete[] m_vertex;
//	delete[] indices;
//}